﻿using FastUtil.Base;
using Quartz;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Hosting;

namespace FastExecutor.FastExecutor.ORG.Design
{
    [DisallowConcurrentExecution]
    public class FastJob : IJob, IInterruptableJob
    {
        public string TaskName { set; get; }
        public string TaskTag { set; get; }
        public FastJob()
        {

        }
        public void Execute(IJobExecutionContext context)
        {
            TaskName = Convert.ToString(context.JobDetail.JobDataMap.Get("TaskName"));
            TaskTag = Convert.ToString(context.JobDetail.JobDataMap.Get("TaskTag"));
            LogUtil.WriteLog("Logs/TaskSchedulerLog", "log_", string.Format("开始执行任务，任务名称：{0}，任务标识：{1}！", TaskName,TaskTag));
            try
            {
                ExecuteJob();
            }
            catch (Exception e)
            {
                LogUtil.WriteLog("Logs/TaskSchedulerLog", "log_", string.Format("执行任务发生异常，任务名称：{0}，任务标识：{1}！", TaskName, TaskTag));
                throw e;
            }
            LogUtil.WriteLog("Logs/TaskSchedulerLog", "log_", string.Format("结束执行任务，任务名称：{0}，任务标识：{1}！", TaskName, TaskTag));

        }
        public void Interrupt()
        {
            LogUtil.WriteLog("Logs/TaskSchedulerLog", "log_", string.Format("任务执行中断，任务名称：{0}，任务标识：{1}！", TaskName, TaskTag));
        }

        public void LogJob(string JobLogContent)
        {
            DirectoryInfo JobLogDir = new DirectoryInfo(HostingEnvironment.MapPath("~/Log/JobLog"));
            if (!JobLogDir.Exists)
            {
                JobLogDir.Create();
            }
            DirectoryInfo JobExecuteLogDir = new DirectoryInfo(HostingEnvironment.MapPath("~/Log/JobLog/" + TaskTag));
            if (!JobExecuteLogDir.Exists)
            {
                JobExecuteLogDir.Create();
            }
            LogUtil.WriteLog("Logs/JobLog/" + TaskTag, "log_", JobLogContent);
        }

        public virtual void ExecuteJob()
        {

        }
    }
}
